
//与APP对接
var android=false;ios=true;
var loginMessage,loginTel,payOK;
	function log(data) {
			var el;
			el = JSON.stringify(data);
			return el;
		}	
   function connectWebViewJavascriptBridge(callback) {
		if (window.WebViewJavascriptBridge) {
			callback(WebViewJavascriptBridge);
		} else {
			document.addEventListener('WebViewJavascriptBridgeReady', function() {
			callback(WebViewJavascriptBridge);
			}, false)
		}	
	}   
	var c=0;	
	function bridgeApp(bridgePushVC,dataJson,bridgeCallback){
		var browser={
    versions:function(){
            var u = navigator.userAgent, app = navigator.appVersion;
            return {         //移动终端浏览器版本信息
                trident: u.indexOf('Trident') > -1, //IE内核
                presto: u.indexOf('Presto') > -1, //opera内核
                webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
                mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
                iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器
                iPad: u.indexOf('iPad') > -1, //是否iPad
                webApp: u.indexOf('Safari') == -1 //是否web应该程序，没有头部与底部
            };
         }(),
         language:(navigator.browserLanguage || navigator.language).toLowerCase()
}
if(browser.versions.ios==true){
	ios=true;
	android=false;	
	connectWebViewJavascriptBridge(function(bridge) {	
        if(c==0){
		  bridge.init(function(message,responseCallback) {
			var appVersion=message.appVersion;
			
         localStorage.appVersion=appVersion;
			var data = {'callback':'none'};
			responseCallback(data);
		})	
		c=1;
		}
	      bridge.callHandler(bridgePushVC,dataJson,function(response) {
				 bridgeCallback(response);
			})	
			if(dataJson.functionName){
		  bridge.registerHandler(dataJson.functionName,dataJson.functionCon)	  
			}
	     })
	}
else if(browser.versions.android==true){
		android=true;
		ios=false;
    if(bridgePushVC=="bridgePushVC"){	
		var vcType=dataJson.vcType;	
		var popNum=Number(dataJson.popNum);
		if(vcType=="0"){
			var url=dataJson.data.url;	
			  window.injs.bridgePushVC(url,0,popNum);
			}
			else if(vcType=="-1"){		
			  window.injs.bridgePushVC("",0,-1);	
			}	
			else if(vcType=="4"){	
			
			  window.injs.bridgePushVC("",4,-1,"");	
			}
			else if(vcType=="7"){
			  var cookId=dataJson.data;			
			  window.injs.bridgePushVC("",7,1,cookId);	
			}	
			else if(vcType=="8"){
				var data=dataJson.data	
			  window.injs.bridgePushVC("",8,-1,data);	
			}				
		}	
		
	else if(bridgePushVC=="bridgeLogin"){	
        window.injs.bridgeLogin();
		loginMessage=function(userid,tel){
			var success=0; 
			if(userid==0&&userid==-1){
			   userid=null;
				}
			if(userid!=null){
			   success=1;
				}
			var response={
			   "userTel":tel,
			   "ID":userid,
			   "success":success
				}					
			 bridgeCallback(response);
			}
		loginMessage2=function(data){
			var success=0; 
			if(data.id==""||data.id==null||data.id=="undefined"||Number(data.id)==0){
			  success=0;
				}
			else{
			   success=1;
				}
			var response=data;	
                response.success=success;			
			 bridgeCallback(response);
			}
		}
	 else if(bridgePushVC=="bridgeBinding"){
		 window.injs.bridgeBinding();
		 loginTel=function(tel){
			var success=0;
			if(tel!=null){
				success=1;
				}
			var response={
				"data":{"userTel":tel},
				"success":success
				}		
			 bridgeCallback(response);
			};
		 }	
	 else if(bridgePushVC=="bridgeJumpToPay"){
		 var type=dataJson.type;
		 var data=JSON.stringify(dataJson.data);
		 window.injs.bridgeJumpToPay(type,data,"payOK")
		 payOK=function(){ 		 
			bridgeCallback();
			 }
		 }
		 
	 else if(bridgePushVC=="userAddrSelect"){
		  window.injs.addressManager();
		  sendAddress=function(site,name,tel){
			var success=0;
			if(tel!=null&&name!=null&&site!=null){
				success=1;
				}
			var response={
				"data":{"address":site,"name":name,"phoneNo":tel},
				"success":success
				}	
			    bridgeCallback(response);
			  }
		 }
	  
	  else if(bridgePushVC=="bridgeShowNavRightInfo"){
		  window.injs.showRightButton(dataJson.icon,"showRight");
		  showRight=function(){
			  if(shareClick==0){
			  Share(dataJson.shareData)}
			  shareClick=1;
			  }
		 }
	  else if(bridgePushVC=="share"){
           
		  window.injs.share(JSON.stringify(dataJson));
		 }
      }
}
 function forcheck(ss,txt){
 var type="^\\d+$";
     var re=new RegExp(type);
       if(ss.match(re)==null)
        {
        commonTckBlack(txt);
        return;
        }
} 
//分享功能
  function Share(shareData){
	 var tr='<section class="origShare">';
         tr=tr+'<ul id="shareUl">';
         tr=tr+'<li id="circle"><p><img src="http://app.yamichefs.com/cook/styles/img/wechatIcon2.png"></p><span>微信朋友圈</span></li>';
         tr=tr+'<li id="friend"><p><img src="http://app.yamichefs.com/cook/styles/img/wechatIcon1.png"></p><span>微信好友</span></li>';
         tr=tr+'<div class="clear"></div>';
         tr=tr+'</ul>';
         tr=tr+'<div class="ShareCancel" id="ShareCancel">取消</div>';
         tr=tr+'</section>';
		 tr=tr+'<section class="origShareBg" id="origShareBg"></section>'
	 $("body").append(tr);
		  $(".origShareBg").fadeIn();
		  $("#shareUl").addClass("shareUlUp")
		  $(".origShare").fadeIn().addClass("bounceInDown")
		  $(".ShareCancel").fadeIn().addClass("slideInDown")		  
		  $("#origButton").css("background","#000");		 
		   params1={
			   "type":"3",
			   "imageUrl":shareData.imageUrl,
                "title":shareData.title,
                "content":shareData.content,
                "url":shareData.url  
			  }	
		   params2={
			   "type":"2",
			   "imageUrl" :shareData.imageUrl,
                "title" : shareData.title,
                "content" : shareData.content,
                "url" : shareData.url  
		   }
	    document.getElementById("circle").addEventListener("touchstart",function(e){ 
	       bridgeApp('share',params1,function(response){
			   	 $(".origShare").addClass("bounceInOut").removeClass("bounceInDown")
		         $("#shareUl").addClass("shareUlOut").removeClass("shareUlUp")
		         $(".ShareCancel").removeClass("slideInDown")
			})
		},true)
	    document.getElementById("friend").addEventListener("touchstart",function(e){ 
		bridgeApp('share',params2,function(response){
			 $(".origShare").addClass("bounceInOut").removeClass("bounceInDown")
		         $("#shareUl").addClass("shareUlOut").removeClass("shareUlUp")
		         $(".ShareCancel").removeClass("slideInDown")	
			})},true); 		 
		  document.getElementById("ShareCancel").addEventListener("click",function(e){		
		  $(".origShare").addClass("bounceInOut").removeClass("bounceInDown");
		  $("#shareUl").addClass("shareUlOut").removeClass("shareUlUp");
		  $(".ShareCancel").removeClass("slideInDown");
		  $(".origShareBg").fadeOut();
		  setTimeout(function(){ $(".origShare").remove();$(".origShareBg").remove();},1000)
		  shareClick=0;
			  })
	    document.getElementById("origShareBg").addEventListener("touchstart",function(){
		  $(".origShare").addClass("bounceInOut").removeClass("bounceInDown")
		   $("#shareUl").addClass("shareUlOut").removeClass("shareUlUp")
		  $(".ShareCancel").removeClass("slideInDown")
	    	})
		document.getElementById("origShareBg").addEventListener("touchend",function(){
		   $(".origShareBg").fadeOut();
		   $(".origShare").fadeOut()
		   setTimeout(function(){$(".origShare").remove();$(".origShareBg").remove();},1000)  
		   shareClick=0;
		})
	  }

//弹出黑框提示，1.5秒后自动关闭
function commonTckBlack(txt,time){
	var td='<div class="common_tck_black">';
	td=td+txt;
	td=td+'</div>';
    var node2=document.createElement("div");
        node2.setAttribute("id","common_tck");	 
		document.body.appendChild(node2); 
		document.getElementById("common_tck").innerHTML=td;	  
		if(time==null){
		   var time=2500;
			}
	    setTimeout(function(){document.body.removeChild(node2)},time)
	}
	
//弹出alert提示（有黑色透明背景），点击确定后关闭
function commonTckAlert(txt,fontSize,lineHeight,url){
	var td='<div class="common_tck_alert">';
    td=td+'<div id="cta_con">'
    td=td+txt;
    td=td+'</div>';
    td=td+'<div id="cta_sure">';
    td=td+"确定";
    td=td+'</div>';
    td=td+'</div>';	
    var node2=document.createElement("div");
        node2.setAttribute("id","common_tck1");	 
		document.body.appendChild(node2); 
		document.getElementById("common_tck1").innerHTML=td;
		document.getElementById("cta_con").style.fontSize=fontSize;
	    document.getElementById("cta_con").style.lineHeight=lineHeight; 
	    document.getElementById("cta_sure").onclick=function(){
		   document.body.removeChild(node2);
		   if(url==undefined) return;
		    window.location.href=url; 
		}		
	}	
/*//点击输入框自动滑动页面避免输入法遮盖输入框。
$("input").focus(function(){
	var scollTop=$(this).offset().top;
	setTimeout(function(){window.scrollTo(0,scollTop-100);
	},500)
	if($("#fixedButton")){
		$("#fixedButton").hide();
	}
})
$("input").blur(function(){
	if($("#fixedButton")){
		$("#fixedButton").show();
	}
})*/